<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>OAuth 2.0 Login :: Spring Security</title>
<link rel="canonical" href="oauth2-login.html">
<link rel="prev" href="index.html">
<link rel="next" href="oauth2-client.html">
<meta name="generator" content="Antora 3.0.0">
<link rel="stylesheet" href="../../../_/css/site.css">
<link href="../../../_/img/favicon.ico" rel='shortcut icon' type='image/vnd.microsoft.icon'>
<link rel="stylesheet" href="../../../_/css/vendor/docsearch.min.css">

<script>var uiRootPath = '../../../_'</script>
</head>
<body class="article">
<header class="header">
<nav class="navbar">
<div class="navbar-brand">
<a class="navbar-item" href="https://spring.io">
<img id="springlogo" class="block" src="../../../_/img/spring-logo.svg" alt="Spring">
</a>
<button class="navbar-burger" data-target="topbar-nav">
<span></span>
<span></span>
<span></span>
</button>
</div>
<div id="topbar-nav" class="navbar-menu">
<div class="navbar-end">
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="oauth2-login.html#">Why Spring</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="https://spring.io/why-spring">Overview</a>
<a class="navbar-item" href="https://spring.io/microservices">Microservices</a>
<a class="navbar-item" href="https://spring.io/reactive">Reactive</a>
<a class="navbar-item" href="https://spring.io/event-driven">Event Driven</a>
<a class="navbar-item" href="https://spring.io/cloud">Cloud</a>
<a class="navbar-item" href="https://spring.io/web-applications">Web Applications</a>
<a class="navbar-item" href="https://spring.io/serverless">Serverless</a>
<a class="navbar-item" href="https://spring.io/batch">Batch</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="oauth2-login.html#">Learn</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="https://spring.io/learn">Overview</a>
<a class="navbar-item" href="https://spring.io/quickstart">Quickstart</a>
<a class="navbar-item" href="https://spring.io/guides">Guides</a>
<a class="navbar-item" href="https://spring.io/blog">Blog</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="oauth2-login.html#">Projects</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="https://spring.io/projects">Overview</a>
<a class="navbar-item" href="https://spring.io/projects/spring-boot">Spring Boot</a>
<a class="navbar-item" href="https://spring.io/projects/spring-framework">Spring Framework</a>
<a class="navbar-item" href="https://spring.io/projects/spring-cloud">Spring Cloud</a>
<a class="navbar-item" href="https://spring.io/projects/spring-cloud-dataflow">Spring Cloud Data Flow</a>
<a class="navbar-item" href="https://spring.io/projects/spring-data">Spring Data</a>
<a class="navbar-item" href="https://spring.io/projects/spring-integration">Spring Integration</a>
<a class="navbar-item" href="https://spring.io/projects/spring-batch">Spring Batch</a>
<a class="navbar-item" href="https://spring.io/projects/spring-security">Spring Security</a>
<a class="navbar-item navbar-item-special" href="https://spring.io/projects">View all projects</a>
<a class="navbar-item" href="https://spring.io/tools">Spring Tools 4</a>
<a class="navbar-item navbar-item-special-2" href="https://start.spring.io">Spring Initializr <svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><polyline points="15 10.94 15 15 1 15 1 1 5.06 1" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="2"></polyline><polyline points="8.93 1 15 1 15 7.07" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="2"></polyline><line x1="15" y1="1" x2="8" y2="8" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="2"></line></svg></a>
</div>
</div>
<a class="navbar-item" href="https://spring.io/training">Training</a>
<a class="navbar-item" href="https://spring.io/support">Support</a>
<div class="navbar-item has-dropdown is-hoverable is-community">
<a class="navbar-link" href="oauth2-login.html#">Community</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="https://spring.io/community">Overview</a>
<a class="navbar-item" href="https://spring.io/events">Events</a>
<a class="navbar-item" href="https://spring.io/team">Team</a>
</div>
</div>
</div>
</div>
<div id="switch-theme">
<input type="checkbox" id="switch-theme-checkbox" />
<label for="switch-theme-checkbox">Dark Theme</label>
</div>
</nav>
</header>
<div class="body">
<div class="nav-container" data-component="ROOT" data-version="5.6.0-RC1">
<aside class="nav">
<div class="panels">
<div class="nav-panel-menu is-active" data-panel="menu">
<nav class="nav-menu">
<h3 class="title"><a href="../../index.html">Spring Security</a></h3>
<ul class="nav-list">
<li class="nav-item" data-depth="0">
<ul class="nav-list">
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../index.html">Overview</a>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../prerequisites.html">Prerequisites</a>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../community.html">Community</a>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../whats-new.html">What&#8217;s New</a>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../getting-spring-security.html">Getting Spring Security</a>
</li>
<li class="nav-item" data-depth="1">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../features/index.html">Features</a>
<ul class="nav-list">
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../features/authentication/index.html">Authentication</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/authentication/password-storage.html">Password Storage</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../features/exploits/index.html">Protection Against Exploits</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/exploits/csrf.html">CSRF</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/exploits/headers.html">HTTP Headers</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/exploits/http.html">HTTP Requests</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../features/integrations/index.html">Integrations</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/integrations/cryptography.html">Cryptography</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/integrations/data.html">Spring Data</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/integrations/concurrency.html">Java&#8217;s Concurrency APIs</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/integrations/jackson.html">Jackson</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../features/integrations/localization.html">Localization</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../modules.html">Project Modules</a>
</li>
<li class="nav-item" data-depth="1">
<a class="nav-link" href="../../samples.html">Samples</a>
</li>
<li class="nav-item" data-depth="1">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../index.html">Servlet Applications</a>
<ul class="nav-list">
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../getting-started.html">Getting Started</a>
</li>
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../architecture.html">Architecture</a>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../authentication/index.html">Authentication</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/architecture.html">Authentication Architecture</a>
</li>
<li class="nav-item" data-depth="3">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../authentication/passwords/index.html">Username/Password</a>
<ul class="nav-list">
<li class="nav-item" data-depth="4">
<button class="nav-item-toggle"></button>
<span class="nav-text">Reading Username/Password</span>
<ul class="nav-list">
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/form.html">Form</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/basic.html">Basic</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/digest.html">Digest</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="4">
<button class="nav-item-toggle"></button>
<span class="nav-text">Password Storage</span>
<ul class="nav-list">
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/in-memory.html">In Memory</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/jdbc.html">JDBC</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/user-details.html">UserDetails</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/user-details-service.html">UserDetailsService</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/password-encoder.html">PasswordEncoder</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/dao-authentication-provider.html">DaoAuthenticationProvider</a>
</li>
<li class="nav-item" data-depth="5">
<a class="nav-link" href="../authentication/passwords/ldap.html">LDAP</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/session-management.html">Session Management</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/rememberme.html">Remember Me</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/openid.html">OpenID</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/anonymous.html">Anonymous</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/preauth.html">Pre-Authentication</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/jaas.html">JAAS</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/cas.html">CAS</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/x509.html">X509</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/runas.html">Run-As</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/logout.html">Logout</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authentication/events.html">Authentication Events</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../authorization/index.html">Authorization</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/architecture.html">Authorization Architecture</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/authorize-requests.html">Authorize HTTP Requests</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/expression-based.html">Expression-Based Access Control</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/secure-objects.html">Secure Object Implementations</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/method-security.html">Method Security</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../authorization/acls.html">Domain Object Security ACLs</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="index.html">OAuth2</a>
<ul class="nav-list">
<li class="nav-item is-current-page" data-depth="3">
<a class="nav-link" href="oauth2-login.html">OAuth2 Log In</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="oauth2-client.html">OAuth2 Client</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="oauth2-resourceserver.html">OAuth2 Resource Server</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../saml2/index.html">SAML2</a>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../exploits/index.html">Protection Against Exploits</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../exploits/csrf.html">Cross Site Request Forgery (CSRF) for Servlet Environments</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../exploits/headers.html">Security HTTP Response Headers</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../exploits/http.html">HTTP</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../exploits/firewall.html">HttpFirewall</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../integrations/index.html">Integrations</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../integrations/servlet-api.html">Servlet APIs</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../integrations/mvc.html">Spring MVC</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../integrations/websocket.html">WebSocket</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../integrations/cors.html">Spring&#8217;s CORS Support</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../integrations/jsp-taglibs.html">JSP Taglib</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<span class="nav-text">Configuration</span>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../configuration/java.html">Java Configuration</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../configuration/kotlin.html">Kotlin Configuration</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../configuration/xml-namespace.html">Namespace Configuration</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../test/index.html">Testing</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../test/method.html">Method Security</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../test/mockmvc.html">MockMvc Support</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../appendix/index.html">Appendix</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../appendix/database-schema.html">Database Schemas</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../appendix/namespace.html">XML Namespace</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../appendix/faq.html">FAQ</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="nav-item" data-depth="1">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../reactive/index.html">Reactive Applications</a>
<ul class="nav-list">
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../../reactive/getting-started.html">Getting Started</a>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<span class="nav-text">Authentication</span>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/authentication/x509.html">X.509 Authentication</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/authentication/logout.html">Logout</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<span class="nav-text">Authorization</span>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/authorization/method.html">EnableReactiveMethodSecurity</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../reactive/oauth2/index.html">OAuth2</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/oauth2/login.html">OAuth 2.0 Login</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/oauth2/oauth2-client.html">OAuth2 Client</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/oauth2/resource-server.html">OAuth 2.0 Resource Server</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/registered-oauth2-authorized-client.html">@RegisteredOAuth2AuthorizedClient</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<a class="nav-link" href="../../reactive/exploits/index.html">Protection Against Exploits</a>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/exploits/csrf.html">CSRF</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/exploits/headers.html">Headers</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/exploits/http.html">HTTP Requests</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<button class="nav-item-toggle"></button>
<span class="nav-text">Integrations</span>
<ul class="nav-list">
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/integrations/cors.html">CORS</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/integrations/rsocket.html">RSocket</a>
</li>
<li class="nav-item" data-depth="3">
<a class="nav-link" href="../../reactive/integrations/webclient.html">WebClient</a>
</li>
</ul>
</li>
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../../reactive/test.html">Testing</a>
</li>
<li class="nav-item" data-depth="2">
<a class="nav-link" href="../../reactive/configuration/webflux.html">WebFlux Security</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</div>
<div class="nav-panel-explore" data-panel="explore">
<div class="context">
<span class="title">Spring Security</span>
<span class="version">5.6.0-RC1</span>
</div>
<ul class="components">
<li class="component is-current">
<a class="title" href="../../../index.html">Spring Security</a>
<ul class="versions">
<li class="version">
<a href="../../../6.0/index.html">6.0.0-SNAPSHOT</a>
</li>
<li class="version">
<a href="../../../6.0.0-M3/index.html">6.0.0-M3</a>
</li>
<li class="version">
<a href="../../../6.0.0-M2/index.html">6.0.0-M2</a>
</li>
<li class="version">
<a href="../../../6.0.0-M1/index.html">6.0.0-M1</a>
</li>
<li class="version">
<a href="../../../5.7/index.html">5.7.0-SNAPSHOT</a>
</li>
<li class="version">
<a href="../../../5.7.0-RC1/index.html">5.7.0-RC1</a>
</li>
<li class="version">
<a href="../../../5.7.0-M3/index.html">5.7.0-M3</a>
</li>
<li class="version">
<a href="../../../5.7.0-M2/index.html">5.7.0-M2</a>
</li>
<li class="version">
<a href="../../../5.7.0-M1/index.html">5.7.0-M1</a>
</li>
<li class="version">
<a href="../../../5.6.4/index.html">5.6.4-SNAPSHOT</a>
</li>
<li class="version is-latest">
<a href="../../../index.html">5.6.3</a>
</li>
<li class="version">
<a href="../../../5.6.2/index.html">5.6.2</a>
</li>
<li class="version">
<a href="../../../5.6.1/index.html">5.6.1</a>
</li>
<li class="version">
<a href="../../../5.6.0/index.html">5.6.0</a>
</li>
<li class="version is-current">
<a href="../../index.html">5.6.0-RC1</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</aside>
</div>
<main class="article">
<div class="toolbar" role="navigation">
<button class="nav-toggle"></button>
<nav class="breadcrumbs" aria-label="breadcrumbs">
<ul>
<li><a href="../../index.html">Spring Security</a></li>
<li><a href="../index.html">Servlet Applications</a></li>
<li><a href="index.html">OAuth2</a></li>
<li><a href="oauth2-login.html">OAuth2 Log In</a></li>
</ul>
</nav>
<div class="search">
<input id="search-input" type="text" placeholder="Search docs">
</div>
<div class="page-versions">
<button class="version-menu-toggle" title="Show other versions of page">5.6.0-RC1</button>
<div class="version-menu">
<a class="version is-missing" href="../../../6.0/index.html">6.0.0-SNAPSHOT</a>
<a class="version is-missing" href="../../../6.0.0-M3/index.html">6.0.0-M3</a>
<a class="version is-missing" href="../../../6.0.0-M2/index.html">6.0.0-M2</a>
<a class="version is-missing" href="../../../6.0.0-M1/index.html">6.0.0-M1</a>
<a class="version is-missing" href="../../../5.7/index.html">5.7.0-SNAPSHOT</a>
<a class="version is-missing" href="../../../5.7.0-RC1/index.html">5.7.0-RC1</a>
<a class="version is-missing" href="../../../5.7.0-M3/index.html">5.7.0-M3</a>
<a class="version is-missing" href="../../../5.7.0-M2/index.html">5.7.0-M2</a>
<a class="version is-missing" href="../../../5.7.0-M1/index.html">5.7.0-M1</a>
<a class="version is-missing" href="../../../5.6.4/index.html">5.6.4-SNAPSHOT</a>
<a class="version is-missing" href="../../../index.html">5.6.3</a>
<a class="version is-missing" href="../../../5.6.2/index.html">5.6.2</a>
<a class="version is-missing" href="../../../5.6.1/index.html">5.6.1</a>
<a class="version is-missing" href="../../../5.6.0/index.html">5.6.0</a>
<a class="version is-current" href="oauth2-login.html">5.6.0-RC1</a>
</div>
</div>
<div class="edit-this-page"><a href="https://github.com/spring-projects/spring-security/blob/5.6.0-RC1/docs/modules/ROOT/pages/servlet/oauth2/oauth2-login.adoc">Edit this Page</a></div>
</div>
<div class="content">
<aside class="toc sidebar" data-title="Contents" data-levels="2">
<div class="toc-menu"></div>
</aside>
<article class="doc">
<div class="admonitionblock important">
<table>
<tbody><tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
<div class="paragraph">
<p> For the latest stable version, please use <a href="../../../index.html">Spring Security 5.6.3</a>!</p>
</div>
</td>
</tr></tbody>
</table>
</div>
<h1 id="page-title" class="page">OAuth 2.0 Login</h1>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>The OAuth 2.0 Login feature provides an application with the capability to have users log in to the application by using their existing account at an OAuth 2.0 Provider (e.g. GitHub) or OpenID Connect 1.0 Provider (such as Google).
OAuth 2.0 Login implements the use cases: "Login with Google" or "Login with GitHub".</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
OAuth 2.0 Login is implemented by using the <strong>Authorization Code Grant</strong>, as specified in the <a href="https://tools.ietf.org/html/rfc6749#section-4.1">OAuth 2.0 Authorization Framework</a> and <a href="https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth">OpenID Connect Core 1.0</a>.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-sample-boot"><a class="anchor" href="oauth2-login.html#oauth2login-sample-boot"></a>Spring Boot 2.x Sample</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Spring Boot 2.x brings full auto-configuration capabilities for OAuth 2.0 Login.</p>
</div>
<div class="paragraph">
<p>This section shows how to configure the <a href="https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/login"><strong>OAuth 2.0 Login sample</strong></a> using <em>Google</em> as the <em>Authentication Provider</em> and covers the following topics:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="oauth2-login.html#oauth2login-sample-initial-setup">Initial setup</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-sample-redirect-uri">Setting the redirect URI</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-sample-application-config">Configure application.yml</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-sample-boot-application">Boot up the application</a></p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="oauth2login-sample-initial-setup"><a class="anchor" href="oauth2-login.html#oauth2login-sample-initial-setup"></a>Initial setup</h3>
<div class="paragraph">
<p>To use Google&#8217;s OAuth 2.0 authentication system for login, you must set up a project in the Google API Console to obtain OAuth 2.0 credentials.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<a href="https://developers.google.com/identity/protocols/OpenIDConnect">Google&#8217;s OAuth 2.0 implementation</a> for authentication conforms to the <a href="https://openid.net/connect/">OpenID Connect 1.0</a> specification and is <a href="https://openid.net/certification/">OpenID Certified</a>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Follow the instructions on the <a href="https://developers.google.com/identity/protocols/OpenIDConnect">OpenID Connect</a> page, starting in the section, "Setting up OAuth 2.0".</p>
</div>
<div class="paragraph">
<p>After completing the "Obtain OAuth 2.0 credentials" instructions, you should have a new OAuth Client with credentials consisting of a Client ID and a Client Secret.</p>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-sample-redirect-uri"><a class="anchor" href="oauth2-login.html#oauth2login-sample-redirect-uri"></a>Setting the redirect URI</h3>
<div class="paragraph">
<p>The redirect URI is the path in the application that the end-user&#8217;s user-agent is redirected back to after they have authenticated with Google and have granted access to the OAuth Client <em>(<a href="oauth2-login.html#oauth2login-sample-initial-setup">created in the previous step</a>)</em> on the Consent page.</p>
</div>
<div class="paragraph">
<p>In the "Set a redirect URI" sub-section, ensure that the <strong>Authorized redirect URIs</strong> field is set to <code><a href="http://localhost:8080/login/oauth2/code/google" class="bare">http://localhost:8080/login/oauth2/code/google</a></code>.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
The default redirect URI template is <code>{baseUrl}/login/oauth2/code/{registrationId}</code>.
The <strong><em>registrationId</em></strong> is a unique identifier for the <a href="oauth2-client.html#oauth2Client-client-registration" class="xref page">ClientRegistration</a>.
</td>
</tr>
</table>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
If the OAuth Client is running behind a proxy server, it is recommended to check <a href="../../features/exploits/http.html#http-proxy-server" class="xref page">Proxy Server Configuration</a> to ensure the application is correctly configured.
Also, see the supported <a href="oauth2-client.html#oauth2Client-auth-code-redirect-uri" class="xref page"> <code>URI</code> template variables</a> for <code>redirect-uri</code>.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-sample-application-config"><a class="anchor" href="oauth2-login.html#oauth2login-sample-application-config"></a>Configure application.yml</h3>
<div class="paragraph">
<p>Now that you have a new OAuth Client with Google, you need to configure the application to use the OAuth Client for the <em>authentication flow</em>.
To do so:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Go to <code>application.yml</code> and set the following configuration:</p>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml">spring:
  security:
    oauth2:
      client:
        registration:	<i class="conum" data-value="1"></i><b>(1)</b>
          google:	<i class="conum" data-value="2"></i><b>(2)</b>
            client-id: google-client-id
            client-secret: google-client-secret</code></pre>
</div>
</div>
<div class="exampleblock">
<div class="title">Example 1. OAuth Client properties</div>
<div class="content">
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td><code>spring.security.oauth2.client.registration</code> is the base property prefix for OAuth Client properties.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Following the base property prefix is the ID for the <a href="oauth2-client.html#oauth2Client-client-registration" class="xref page">ClientRegistration</a>, such as google.</td>
</tr>
</table>
</div>
</div>
</div>
</li>
<li>
<p>Replace the values in the <code>client-id</code> and <code>client-secret</code> property with the OAuth 2.0 credentials you created earlier.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-sample-boot-application"><a class="anchor" href="oauth2-login.html#oauth2login-sample-boot-application"></a>Boot up the application</h3>
<div class="paragraph">
<p>Launch the Spring Boot 2.x sample and go to <code><a href="http://localhost:8080" class="bare">http://localhost:8080</a></code>.
You are then redirected to the default <em>auto-generated</em> login page, which displays a link for Google.</p>
</div>
<div class="paragraph">
<p>Click on the Google link, and you are then redirected to Google for authentication.</p>
</div>
<div class="paragraph">
<p>After authenticating with your Google account credentials, the next page presented to you is the Consent screen.
The Consent screen asks you to either allow or deny access to the OAuth Client you created earlier.
Click <strong>Allow</strong> to authorize the OAuth Client to access your email address and basic profile information.</p>
</div>
<div class="paragraph">
<p>At this point, the OAuth Client retrieves your email address and basic profile information from the <a href="https://openid.net/specs/openid-connect-core-1_0.html#UserInfo">UserInfo Endpoint</a> and establishes an authenticated session.</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-boot-property-mappings"><a class="anchor" href="oauth2-login.html#oauth2login-boot-property-mappings"></a>Spring Boot 2.x Property Mappings</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The following table outlines the mapping of the Spring Boot 2.x OAuth Client properties to the <a href="oauth2-client.html#oauth2Client-client-registration" class="xref page">ClientRegistration</a> properties.</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Spring Boot 2.x</th>
<th class="tableblock halign-left valign-top">ClientRegistration</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em></code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>registrationId</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.client-id</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>clientId</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.client-secret</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>clientSecret</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.client-authentication-method</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>clientAuthenticationMethod</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.authorization-grant-type</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>authorizationGrantType</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.redirect-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>redirectUri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.scope</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>scopes</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.registration.<em>[registrationId]</em>.client-name</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>clientName</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.authorization-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.authorizationUri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.token-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.tokenUri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.jwk-set-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.jwkSetUri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.issuer-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.issuerUri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.user-info-uri</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.userInfoEndpoint.uri</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.user-info-authentication-method</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.userInfoEndpoint.authenticationMethod</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>spring.security.oauth2.client.provider.<em>[providerId]</em>.user-name-attribute</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>providerDetails.userInfoEndpoint.userNameAttributeName</code></p></td>
</tr>
</tbody>
</table>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
A <code>ClientRegistration</code> can be initially configured using discovery of an OpenID Connect Provider&#8217;s <a href="https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig">Configuration endpoint</a> or an Authorization Server&#8217;s <a href="https://tools.ietf.org/html/rfc8414#section-3">Metadata endpoint</a>, by specifying the <code>spring.security.oauth2.client.provider.<em>[providerId]</em>.issuer-uri</code> property.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-common-oauth2-provider"><a class="anchor" href="oauth2-login.html#oauth2login-common-oauth2-provider"></a>CommonOAuth2Provider</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>CommonOAuth2Provider</code> pre-defines a set of default client properties for a number of well known providers: Google, GitHub, Facebook, and Okta.</p>
</div>
<div class="paragraph">
<p>For example, the <code>authorization-uri</code>, <code>token-uri</code>, and <code>user-info-uri</code> do not change often for a Provider.
Therefore, it makes sense to provide default values in order to reduce the required configuration.</p>
</div>
<div class="paragraph">
<p>As demonstrated previously, when we <a href="oauth2-login.html#oauth2login-sample-application-config">configured a Google client</a>, only the <code>client-id</code> and <code>client-secret</code> properties are required.</p>
</div>
<div class="paragraph">
<p>The following listing shows an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml">spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: google-client-id
            client-secret: google-client-secret</code></pre>
</div>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
The auto-defaulting of client properties works seamlessly here because the <code>registrationId</code> (<code>google</code>) matches the <code>GOOGLE</code> <code>enum</code> (case-insensitive) in <code>CommonOAuth2Provider</code>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>For cases where you may want to specify a different <code>registrationId</code>, such as <code>google-login</code>, you can still leverage auto-defaulting of client properties by configuring the <code>provider</code> property.</p>
</div>
<div class="paragraph">
<p>The following listing shows an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml">spring:
  security:
    oauth2:
      client:
        registration:
          google-login:	<i class="conum" data-value="1"></i><b>(1)</b>
            provider: google	<i class="conum" data-value="2"></i><b>(2)</b>
            client-id: google-client-id
            client-secret: google-client-secret</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The <code>registrationId</code> is set to <code>google-login</code>.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>The <code>provider</code> property is set to <code>google</code>, which will leverage the auto-defaulting of client properties set in <code>CommonOAuth2Provider.GOOGLE.getBuilder()</code>.</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-custom-provider-properties"><a class="anchor" href="oauth2-login.html#oauth2login-custom-provider-properties"></a>Configuring Custom Provider Properties</h2>
<div class="sectionbody">
<div class="paragraph">
<p>There are some OAuth 2.0 Providers that support multi-tenancy, which results in different protocol endpoints for each tenant (or sub-domain).</p>
</div>
<div class="paragraph">
<p>For example, an OAuth Client registered with Okta is assigned to a specific sub-domain and have their own protocol endpoints.</p>
</div>
<div class="paragraph">
<p>For these cases, Spring Boot 2.x provides the following base property for configuring custom provider properties: <code>spring.security.oauth2.client.provider.<em>[providerId]</em></code>.</p>
</div>
<div class="paragraph">
<p>The following listing shows an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml">spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
        provider:
          okta:	<i class="conum" data-value="1"></i><b>(1)</b>
            authorization-uri: https://your-subdomain.oktapreview.com/oauth2/v1/authorize
            token-uri: https://your-subdomain.oktapreview.com/oauth2/v1/token
            user-info-uri: https://your-subdomain.oktapreview.com/oauth2/v1/userinfo
            user-name-attribute: sub
            jwk-set-uri: https://your-subdomain.oktapreview.com/oauth2/v1/keys</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>The base property (<code>spring.security.oauth2.client.provider.okta</code>) allows for custom configuration of protocol endpoint locations.</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-override-boot-autoconfig"><a class="anchor" href="oauth2-login.html#oauth2login-override-boot-autoconfig"></a>Overriding Spring Boot 2.x Auto-configuration</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The Spring Boot 2.x auto-configuration class for OAuth Client support is <code>OAuth2ClientAutoConfiguration</code>.</p>
</div>
<div class="paragraph">
<p>It performs the following tasks:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Registers a <code>ClientRegistrationRepository</code> <code>@Bean</code> composed of <code>ClientRegistration</code>(s) from the configured OAuth Client properties.</p>
</li>
<li>
<p>Provides a <code>WebSecurityConfigurerAdapter</code> <code>@Configuration</code> and enables OAuth 2.0 Login through <code>httpSecurity.oauth2Login()</code>.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If you need to override the auto-configuration based on your specific requirements, you may do so in the following ways:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="oauth2-login.html#oauth2login-register-clientregistrationrepository-bean">Register a ClientRegistrationRepository @Bean</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-provide-websecurityconfigureradapter">Provide a WebSecurityConfigurerAdapter</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-completely-override-autoconfiguration">Completely Override the Auto-configuration</a></p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="oauth2login-register-clientregistrationrepository-bean"><a class="anchor" href="oauth2-login.html#oauth2login-register-clientregistrationrepository-bean"></a>Register a ClientRegistrationRepository @Bean</h3>
<div class="paragraph">
<p>The following example shows how to register a <code>ClientRegistrationRepository</code> <code>@Bean</code>:</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Configuration
public class OAuth2LoginConfig {

	@Bean
	public ClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
	}

	private ClientRegistration googleClientRegistration() {
		return ClientRegistration.withRegistrationId("google")
			.clientId("google-client-id")
			.clientSecret("google-client-secret")
			.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
			.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
			.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
			.scope("openid", "profile", "email", "address", "phone")
			.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
			.tokenUri("https://www.googleapis.com/oauth2/v4/token")
			.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
			.userNameAttributeName(IdTokenClaimNames.SUB)
			.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
			.clientName("Google")
			.build();
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@Configuration
class OAuth2LoginConfig {
    @Bean
    fun clientRegistrationRepository(): ClientRegistrationRepository {
        return InMemoryClientRegistrationRepository(googleClientRegistration())
    }

    private fun googleClientRegistration(): ClientRegistration {
        return ClientRegistration.withRegistrationId("google")
                .clientId("google-client-id")
                .clientSecret("google-client-secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("openid", "profile", "email", "address", "phone")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .tokenUri("https://www.googleapis.com/oauth2/v4/token")
                .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
                .userNameAttributeName(IdTokenClaimNames.SUB)
                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                .clientName("Google")
                .build()
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-provide-websecurityconfigureradapter"><a class="anchor" href="oauth2-login.html#oauth2login-provide-websecurityconfigureradapter"></a>Provide a WebSecurityConfigurerAdapter</h3>
<div class="paragraph">
<p>The following example shows how to provide a <code>WebSecurityConfigurerAdapter</code> with <code>@EnableWebSecurity</code> and enable OAuth 2.0 login through <code>httpSecurity.oauth2Login()</code>:</p>
</div>
<div class="exampleblock">
<div class="title">Example 2. OAuth2 Login Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests(authorize -&gt; authorize
				.anyRequest().authenticated()
			)
			.oauth2Login(withDefaults());
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            authorizeRequests {
                authorize(anyRequest, authenticated)
            }
            oauth2Login { }
        }
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-completely-override-autoconfiguration"><a class="anchor" href="oauth2-login.html#oauth2login-completely-override-autoconfiguration"></a>Completely Override the Auto-configuration</h3>
<div class="paragraph">
<p>The following example shows how to completely override the auto-configuration by registering a <code>ClientRegistrationRepository</code> <code>@Bean</code> and providing a <code>WebSecurityConfigurerAdapter</code>.</p>
</div>
<div class="exampleblock">
<div class="title">Example 3. Overriding the auto-configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Configuration
public class OAuth2LoginConfig {

	@EnableWebSecurity
	public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http
				.authorizeRequests(authorize -&gt; authorize
					.anyRequest().authenticated()
				)
				.oauth2Login(withDefaults());
		}
	}

	@Bean
	public ClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
	}

	private ClientRegistration googleClientRegistration() {
		return ClientRegistration.withRegistrationId("google")
			.clientId("google-client-id")
			.clientSecret("google-client-secret")
			.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
			.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
			.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
			.scope("openid", "profile", "email", "address", "phone")
			.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
			.tokenUri("https://www.googleapis.com/oauth2/v4/token")
			.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
			.userNameAttributeName(IdTokenClaimNames.SUB)
			.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
			.clientName("Google")
			.build();
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@Configuration
class OAuth2LoginConfig {

    @EnableWebSecurity
    class OAuth2LoginSecurityConfig: WebSecurityConfigurerAdapter() {

        override fun configure(http: HttpSecurity) {
            http {
                authorizeRequests {
                    authorize(anyRequest, authenticated)
                }
                oauth2Login { }
            }
        }
    }

    @Bean
    fun clientRegistrationRepository(): ClientRegistrationRepository {
        return InMemoryClientRegistrationRepository(googleClientRegistration())
    }

    private fun googleClientRegistration(): ClientRegistration {
        return ClientRegistration.withRegistrationId("google")
                .clientId("google-client-id")
                .clientSecret("google-client-secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("openid", "profile", "email", "address", "phone")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .tokenUri("https://www.googleapis.com/oauth2/v4/token")
                .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
                .userNameAttributeName(IdTokenClaimNames.SUB)
                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                .clientName("Google")
                .build()
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-javaconfig-wo-boot"><a class="anchor" href="oauth2-login.html#oauth2login-javaconfig-wo-boot"></a>Java Configuration without Spring Boot 2.x</h2>
<div class="sectionbody">
<div class="paragraph">
<p>If you are not able to use Spring Boot 2.x and would like to configure one of the pre-defined providers in <code>CommonOAuth2Provider</code> (for example, Google), apply the following configuration:</p>
</div>
<div class="exampleblock">
<div class="title">Example 4. OAuth2 Login Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Configuration
public class OAuth2LoginConfig {

	@EnableWebSecurity
	public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http
				.authorizeRequests(authorize -&gt; authorize
					.anyRequest().authenticated()
				)
				.oauth2Login(withDefaults());
		}
	}

	@Bean
	public ClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
	}

	@Bean
	public OAuth2AuthorizedClientService authorizedClientService(
			ClientRegistrationRepository clientRegistrationRepository) {
		return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);
	}

	@Bean
	public OAuth2AuthorizedClientRepository authorizedClientRepository(
			OAuth2AuthorizedClientService authorizedClientService) {
		return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);
	}

	private ClientRegistration googleClientRegistration() {
		return CommonOAuth2Provider.GOOGLE.getBuilder("google")
			.clientId("google-client-id")
			.clientSecret("google-client-secret")
			.build();
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@Configuration
open class OAuth2LoginConfig {
    @EnableWebSecurity
    open class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
        override fun configure(http: HttpSecurity) {
            http {
                authorizeRequests {
                    authorize(anyRequest, authenticated)
                }
                oauth2Login { }
            }
        }
    }

    @Bean
    open fun clientRegistrationRepository(): ClientRegistrationRepository {
        return InMemoryClientRegistrationRepository(googleClientRegistration())
    }

    @Bean
    open fun authorizedClientService(
        clientRegistrationRepository: ClientRegistrationRepository?
    ): OAuth2AuthorizedClientService {
        return InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository)
    }

    @Bean
    open fun authorizedClientRepository(
        authorizedClientService: OAuth2AuthorizedClientService?
    ): OAuth2AuthorizedClientRepository {
        return AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService)
    }

    private fun googleClientRegistration(): ClientRegistration {
        return CommonOAuth2Provider.GOOGLE.getBuilder("google")
            .clientId("google-client-id")
            .clientSecret("google-client-secret")
            .build()
    }
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Xml</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http auto-config="true"&gt;
	&lt;intercept-url pattern="/**" access="authenticated"/&gt;
	&lt;oauth2-login authorized-client-repository-ref="authorizedClientRepository"/&gt;
&lt;/http&gt;

&lt;client-registrations&gt;
	&lt;client-registration registration-id="google"
						 client-id="google-client-id"
						 client-secret="google-client-secret"
						 provider-id="google"/&gt;
&lt;/client-registrations&gt;

&lt;b:bean id="authorizedClientService"
		class="org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService"
		autowire="constructor"/&gt;

&lt;b:bean id="authorizedClientRepository"
		class="org.springframework.security.oauth2.client.web.AuthenticatedPrincipalOAuth2AuthorizedClientRepository"&gt;
	&lt;b:constructor-arg ref="authorizedClientService"/&gt;
&lt;/b:bean&gt;</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="oauth2login-advanced"><a class="anchor" href="oauth2-login.html#oauth2login-advanced"></a>Advanced Configuration</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>HttpSecurity.oauth2Login()</code> provides a number of configuration options for customizing OAuth 2.0 Login.
The main configuration options are grouped into their protocol endpoint counterparts.</p>
</div>
<div class="paragraph">
<p>For example, <code>oauth2Login().authorizationEndpoint()</code> allows configuring the <em>Authorization Endpoint</em>, whereas <code>oauth2Login().tokenEndpoint()</code> allows configuring the <em>Token Endpoint</em>.</p>
</div>
<div class="paragraph">
<p>The following code shows an example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 5. Advanced OAuth2 Login Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .authorizationEndpoint(authorization -&gt; authorization
			            ...
			    )
			    .redirectionEndpoint(redirection -&gt; redirection
			            ...
			    )
			    .tokenEndpoint(token -&gt; token
			            ...
			    )
			    .userInfoEndpoint(userInfo -&gt; userInfo
			            ...
			    )
			);
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                authorizationEndpoint {
                    ...
                }
                redirectionEndpoint {
                    ...
                }
                tokenEndpoint {
                    ...
                }
                userInfoEndpoint {
                    ...
                }
            }
        }
    }
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The main goal of the <code>oauth2Login()</code> DSL was to closely align with the naming, as defined in the specifications.</p>
</div>
<div class="paragraph">
<p>The OAuth 2.0 Authorization Framework defines the <a href="https://tools.ietf.org/html/rfc6749#section-3">Protocol Endpoints</a> as follows:</p>
</div>
<div class="paragraph">
<p>The authorization process utilizes two authorization server endpoints (HTTP resources):</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Authorization Endpoint: Used by the client to obtain authorization from the resource owner via user-agent redirection.</p>
</li>
<li>
<p>Token Endpoint: Used by the client to exchange an authorization grant for an access token, typically with client authentication.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>As well as one client endpoint:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Redirection Endpoint: Used by the authorization server to return responses containing authorization credentials to the client via the resource owner user-agent.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The OpenID Connect Core 1.0 specification defines the <a href="https://openid.net/specs/openid-connect-core-1_0.html#UserInfo">UserInfo Endpoint</a> as follows:</p>
</div>
<div class="paragraph">
<p>The UserInfo Endpoint is an OAuth 2.0 Protected Resource that returns claims about the authenticated end-user.
To obtain the requested claims about the end-user, the client makes a request to the UserInfo Endpoint by using an access token obtained through OpenID Connect Authentication.
These claims are normally represented by a JSON object that contains a collection of name-value pairs for the claims.</p>
</div>
<div class="paragraph">
<p>The following code shows the complete configuration options available for the <code>oauth2Login()</code> DSL:</p>
</div>
<div class="exampleblock">
<div class="title">Example 6. OAuth2 Login Configuration Options</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .clientRegistrationRepository(this.clientRegistrationRepository())
			    .authorizedClientRepository(this.authorizedClientRepository())
			    .authorizedClientService(this.authorizedClientService())
			    .loginPage("/login")
			    .authorizationEndpoint(authorization -&gt; authorization
			        .baseUri(this.authorizationRequestBaseUri())
			        .authorizationRequestRepository(this.authorizationRequestRepository())
			        .authorizationRequestResolver(this.authorizationRequestResolver())
			    )
			    .redirectionEndpoint(redirection -&gt; redirection
			        .baseUri(this.authorizationResponseBaseUri())
			    )
			    .tokenEndpoint(token -&gt; token
			        .accessTokenResponseClient(this.accessTokenResponseClient())
			    )
			    .userInfoEndpoint(userInfo -&gt; userInfo
			        .userAuthoritiesMapper(this.userAuthoritiesMapper())
			        .userService(this.oauth2UserService())
			        .oidcUserService(this.oidcUserService())
			    )
			);
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                clientRegistrationRepository = clientRegistrationRepository()
                authorizedClientRepository = authorizedClientRepository()
                authorizedClientService = authorizedClientService()
                loginPage = "/login"
                authorizationEndpoint {
                    baseUri = authorizationRequestBaseUri()
                    authorizationRequestRepository = authorizationRequestRepository()
                    authorizationRequestResolver = authorizationRequestResolver()
                }
                redirectionEndpoint {
                    baseUri = authorizationResponseBaseUri()
                }
                tokenEndpoint {
                    accessTokenResponseClient = accessTokenResponseClient()
                }
                userInfoEndpoint {
                    userAuthoritiesMapper = userAuthoritiesMapper()
                    userService = oauth2UserService()
                    oidcUserService = oidcUserService()
                }
            }
        }
    }
}</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>In addition to the <code>oauth2Login()</code> DSL, XML configuration is also supported.</p>
</div>
<div class="paragraph">
<p>The following code shows the complete configuration options available in the <a href="../appendix/namespace.html#nsa-oauth2-login" class="xref page"> security namespace</a>:</p>
</div>
<div class="exampleblock">
<div class="title">Example 7. OAuth2 Login XML Configuration Options</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http&gt;
	&lt;oauth2-login client-registration-repository-ref="clientRegistrationRepository"
				  authorized-client-repository-ref="authorizedClientRepository"
				  authorized-client-service-ref="authorizedClientService"
				  authorization-request-repository-ref="authorizationRequestRepository"
				  authorization-request-resolver-ref="authorizationRequestResolver"
				  access-token-response-client-ref="accessTokenResponseClient"
				  user-authorities-mapper-ref="userAuthoritiesMapper"
				  user-service-ref="oauth2UserService"
				  oidc-user-service-ref="oidcUserService"
				  login-processing-url="/login/oauth2/code/*"
				  login-page="/login"
				  authentication-success-handler-ref="authenticationSuccessHandler"
				  authentication-failure-handler-ref="authenticationFailureHandler"
				  jwt-decoder-factory-ref="jwtDecoderFactory"/&gt;
&lt;/http&gt;</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>The following sections go into more detail on each of the configuration options available:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-login-page">OAuth 2.0 Login Page</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-redirection-endpoint">Redirection Endpoint</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-userinfo-endpoint">UserInfo Endpoint</a></p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="oauth2login-advanced-login-page"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-login-page"></a>OAuth 2.0 Login Page</h3>
<div class="paragraph">
<p>By default, the OAuth 2.0 Login Page is auto-generated by the <code>DefaultLoginPageGeneratingFilter</code>.
The default login page shows each configured OAuth Client with its <code>ClientRegistration.clientName</code> as a link, which is capable of initiating the Authorization Request (or OAuth 2.0 Login).</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
In order for <code>DefaultLoginPageGeneratingFilter</code> to show links for configured OAuth Clients, the registered <code>ClientRegistrationRepository</code> needs to also implement <code>Iterable&lt;ClientRegistration&gt;</code>.
See <code>InMemoryClientRegistrationRepository</code> for reference.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The link&#8217;s destination for each OAuth Client defaults to the following:</p>
</div>
<div class="paragraph">
<p><code>OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/{registrationId}"</code></p>
</div>
<div class="paragraph">
<p>The following line shows an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-html hljs" data-lang="html">&lt;a href="/oauth2/authorization/google"&gt;Google&lt;/a&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>To override the default login page, configure <code>oauth2Login().loginPage()</code> and (optionally) <code>oauth2Login().authorizationEndpoint().baseUri()</code>.</p>
</div>
<div class="paragraph">
<p>The following listing shows an example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 8. OAuth2 Login Page Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .loginPage("/login/oauth2")
			    ...
			    .authorizationEndpoint(authorization -&gt; authorization
			        .baseUri("/login/oauth2/authorization")
			        ...
			    )
			);
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                loginPage = "/login/oauth2"
                authorizationEndpoint {
                    baseUri = "/login/oauth2/authorization"
                }
            }
        }
    }
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Xml</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http&gt;
	&lt;oauth2-login login-page="/login/oauth2"
				  ...
    /&gt;
&lt;/http&gt;</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
You need to provide a <code>@Controller</code> with a <code>@RequestMapping("/login/oauth2")</code> that is capable of rendering the custom login page.
</td>
</tr>
</table>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
<div class="paragraph">
<p>As noted earlier, configuring <code>oauth2Login().authorizationEndpoint().baseUri()</code> is optional.
However, if you choose to customize it, ensure the link to each OAuth Client matches the <code>authorizationEndpoint().baseUri()</code>.</p>
</div>
<div class="paragraph">
<p>The following line shows an example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-html hljs" data-lang="html">&lt;a href="/login/oauth2/authorization/google"&gt;Google&lt;/a&gt;</code></pre>
</div>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-advanced-redirection-endpoint"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-redirection-endpoint"></a>Redirection Endpoint</h3>
<div class="paragraph">
<p>The Redirection Endpoint is used by the Authorization Server for returning the Authorization Response (which contains the authorization credentials) to the client via the Resource Owner user-agent.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
OAuth 2.0 Login leverages the Authorization Code Grant.
Therefore, the authorization credential is the authorization code.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The default Authorization Response <code>baseUri</code> (redirection endpoint) is <code><strong>/login/oauth2/code/</strong>*</code>, which is defined in <code>OAuth2LoginAuthenticationFilter.DEFAULT_FILTER_PROCESSES_URI</code>.</p>
</div>
<div class="paragraph">
<p>If you would like to customize the Authorization Response <code>baseUri</code>, configure it as shown in the following example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 9. Redirection Endpoint Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .redirectionEndpoint(redirection -&gt; redirection
			        .baseUri("/login/oauth2/callback/*")
			        ...
			    )
			);
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                redirectionEndpoint {
                    baseUri = "/login/oauth2/callback/*"
                }
            }
        }
    }
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Xml</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http&gt;
	&lt;oauth2-login login-processing-url="/login/oauth2/callback/*"
				  ...
    /&gt;
&lt;/http&gt;</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
<div class="paragraph">
<p>You also need to ensure the <code>ClientRegistration.redirectUri</code> matches the custom Authorization Response <code>baseUri</code>.</p>
</div>
<div class="paragraph">
<p>The following listing shows an example:</p>
</div>
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">return CommonOAuth2Provider.GOOGLE.getBuilder("google")
	.clientId("google-client-id")
	.clientSecret("google-client-secret")
	.redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
	.build();</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">return CommonOAuth2Provider.GOOGLE.getBuilder("google")
    .clientId("google-client-id")
    .clientSecret("google-client-secret")
    .redirectUri("{baseUrl}/login/oauth2/callback/{registrationId}")
    .build()</code></pre>
</div>
</div>
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-advanced-userinfo-endpoint"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-userinfo-endpoint"></a>UserInfo Endpoint</h3>
<div class="paragraph">
<p>The UserInfo Endpoint includes a number of configuration options, as described in the following sub-sections:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-map-authorities">Mapping User Authorities</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-oauth2-user-service">OAuth 2.0 UserService</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-oidc-user-service">OpenID Connect 1.0 UserService</a></p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="oauth2login-advanced-map-authorities"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-map-authorities"></a>Mapping User Authorities</h4>
<div class="paragraph">
<p>After the user successfully authenticates with the OAuth 2.0 Provider, the <code>OAuth2User.getAuthorities()</code> (or <code>OidcUser.getAuthorities()</code>) may be mapped to a new set of <code>GrantedAuthority</code> instances, which will be supplied to <code>OAuth2AuthenticationToken</code> when completing the authentication.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
<code>OAuth2AuthenticationToken.getAuthorities()</code> is used for authorizing requests, such as in <code>hasRole('USER')</code> or <code>hasRole('ADMIN')</code>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>There are a couple of options to choose from when mapping user authorities:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-map-authorities-grantedauthoritiesmapper">Using a GrantedAuthoritiesMapper</a></p>
</li>
<li>
<p><a href="oauth2-login.html#oauth2login-advanced-map-authorities-oauth2userservice">Delegation-based strategy with OAuth2UserService</a></p>
</li>
</ul>
</div>
<div class="sect4">
<h5 id="oauth2login-advanced-map-authorities-grantedauthoritiesmapper"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-map-authorities-grantedauthoritiesmapper"></a>Using a GrantedAuthoritiesMapper</h5>
<div class="paragraph">
<p>Provide an implementation of <code>GrantedAuthoritiesMapper</code> and configure it as shown in the following example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 10. Granted Authorities Mapper Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .userInfoEndpoint(userInfo -&gt; userInfo
			        .userAuthoritiesMapper(this.userAuthoritiesMapper())
			        ...
			    )
			);
	}

	private GrantedAuthoritiesMapper userAuthoritiesMapper() {
		return (authorities) -&gt; {
			Set&lt;GrantedAuthority&gt; mappedAuthorities = new HashSet&lt;&gt;();

			authorities.forEach(authority -&gt; {
				if (OidcUserAuthority.class.isInstance(authority)) {
					OidcUserAuthority oidcUserAuthority = (OidcUserAuthority)authority;

					OidcIdToken idToken = oidcUserAuthority.getIdToken();
					OidcUserInfo userInfo = oidcUserAuthority.getUserInfo();

					// Map the claims found in idToken and/or userInfo
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				} else if (OAuth2UserAuthority.class.isInstance(authority)) {
					OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority)authority;

					Map&lt;String, Object&gt; userAttributes = oauth2UserAuthority.getAttributes();

					// Map the attributes found in userAttributes
					// to one or more GrantedAuthority's and add it to mappedAuthorities

				}
			});

			return mappedAuthorities;
		};
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                userInfoEndpoint {
                    userAuthoritiesMapper = userAuthoritiesMapper()
                }
            }
        }
    }

    private fun userAuthoritiesMapper(): GrantedAuthoritiesMapper = GrantedAuthoritiesMapper { authorities: Collection&lt;GrantedAuthority&gt; -&gt;
        val mappedAuthorities = emptySet&lt;GrantedAuthority&gt;()

        authorities.forEach { authority -&gt;
            if (authority is OidcUserAuthority) {
                val idToken = authority.idToken
                val userInfo = authority.userInfo
                // Map the claims found in idToken and/or userInfo
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            } else if (authority is OAuth2UserAuthority) {
                val userAttributes = authority.attributes
                // Map the attributes found in userAttributes
                // to one or more GrantedAuthority's and add it to mappedAuthorities
            }
        }

        mappedAuthorities
    }
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Xml</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http&gt;
	&lt;oauth2-login user-authorities-mapper-ref="userAuthoritiesMapper"
				  ...
    /&gt;
&lt;/http&gt;</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>Alternatively, you may register a <code>GrantedAuthoritiesMapper</code> <code>@Bean</code> to have it automatically applied to the configuration, as shown in the following example:</p>
</div>
<div class="exampleblock">
<div class="title">Example 11. Granted Authorities Mapper Bean Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
		    .oauth2Login(withDefaults());
	}

	@Bean
	public GrantedAuthoritiesMapper userAuthoritiesMapper() {
		...
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login { }
        }
    }

    @Bean
    fun userAuthoritiesMapper(): GrantedAuthoritiesMapper {
        ...
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect4">
<h5 id="oauth2login-advanced-map-authorities-oauth2userservice"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-map-authorities-oauth2userservice"></a>Delegation-based strategy with OAuth2UserService</h5>
<div class="paragraph">
<p>This strategy is advanced compared to using a <code>GrantedAuthoritiesMapper</code>, however, it&#8217;s also more flexible as it gives you access to the <code>OAuth2UserRequest</code> and <code>OAuth2User</code> (when using an OAuth 2.0 UserService) or <code>OidcUserRequest</code> and <code>OidcUser</code> (when using an OpenID Connect 1.0 UserService).</p>
</div>
<div class="paragraph">
<p>The <code>OAuth2UserRequest</code> (and <code>OidcUserRequest</code>) provides you access to the associated <code>OAuth2AccessToken</code>, which is very useful in the cases where the <em>delegator</em> needs to fetch authority information from a protected resource before it can map the custom authorities for the user.</p>
</div>
<div class="paragraph">
<p>The following example shows how to implement and configure a delegation-based strategy using an OpenID Connect 1.0 UserService:</p>
</div>
<div class="exampleblock">
<div class="title">Example 12. OAuth2UserService Configuration</div>
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .userInfoEndpoint(userInfo -&gt; userInfo
			        .oidcUserService(this.oidcUserService())
			        ...
			    )
			);
	}

	private OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; oidcUserService() {
		final OidcUserService delegate = new OidcUserService();

		return (userRequest) -&gt; {
			// Delegate to the default implementation for loading a user
			OidcUser oidcUser = delegate.loadUser(userRequest);

			OAuth2AccessToken accessToken = userRequest.getAccessToken();
			Set&lt;GrantedAuthority&gt; mappedAuthorities = new HashSet&lt;&gt;();

			// TODO
			// 1) Fetch the authority information from the protected resource using accessToken
			// 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities

			// 3) Create a copy of oidcUser but use the mappedAuthorities instead
			oidcUser = new DefaultOidcUser(mappedAuthorities, oidcUser.getIdToken(), oidcUser.getUserInfo());

			return oidcUser;
		};
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                userInfoEndpoint {
                    oidcUserService = oidcUserService()
                }
            }
        }
    }

    @Bean
    fun oidcUserService(): OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; {
        val delegate = OidcUserService()

        return OAuth2UserService { userRequest -&gt;
            // Delegate to the default implementation for loading a user
            var oidcUser = delegate.loadUser(userRequest)

            val accessToken = userRequest.accessToken
            val mappedAuthorities = HashSet&lt;GrantedAuthority&gt;()

            // TODO
            // 1) Fetch the authority information from the protected resource using accessToken
            // 2) Map the authority information to one or more GrantedAuthority's and add it to mappedAuthorities
            // 3) Create a copy of oidcUser but use the mappedAuthorities instead
            oidcUser = DefaultOidcUser(mappedAuthorities, oidcUser.idToken, oidcUser.userInfo)

            oidcUser
        }
    }
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Xml</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;http&gt;
	&lt;oauth2-login oidc-user-service-ref="oidcUserService"
				  ...
    /&gt;
&lt;/http&gt;</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="oauth2login-advanced-oauth2-user-service"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-oauth2-user-service"></a>OAuth 2.0 UserService</h4>
<div class="paragraph">
<p><code>DefaultOAuth2UserService</code> is an implementation of an <code>OAuth2UserService</code> that supports standard OAuth 2.0 Provider&#8217;s.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<code>OAuth2UserService</code> obtains the user attributes of the end-user (the resource owner) from the UserInfo Endpoint (by using the access token granted to the client during the authorization flow) and returns an <code>AuthenticatedPrincipal</code> in the form of an <code>OAuth2User</code>.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><code>DefaultOAuth2UserService</code> uses a <code>RestOperations</code> when requesting the user attributes at the UserInfo Endpoint.</p>
</div>
<div class="paragraph">
<p>If you need to customize the pre-processing of the UserInfo Request, you can provide <code>DefaultOAuth2UserService.setRequestEntityConverter()</code> with a custom <code>Converter&lt;OAuth2UserRequest, RequestEntity&lt;?&gt;&gt;</code>.
The default implementation <code>OAuth2UserRequestEntityConverter</code> builds a <code>RequestEntity</code> representation of a UserInfo Request that sets the <code>OAuth2AccessToken</code> in the <code>Authorization</code> header by default.</p>
</div>
<div class="paragraph">
<p>On the other end, if you need to customize the post-handling of the UserInfo Response, you will need to provide <code>DefaultOAuth2UserService.setRestOperations()</code> with a custom configured <code>RestOperations</code>.
The default <code>RestOperations</code> is configured as follows:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());</code></pre>
</div>
</div>
<div class="paragraph">
<p><code>OAuth2ErrorResponseErrorHandler</code> is a <code>ResponseErrorHandler</code> that can handle an OAuth 2.0 Error (400 Bad Request).
It uses an <code>OAuth2ErrorHttpMessageConverter</code> for converting the OAuth 2.0 Error parameters to an <code>OAuth2Error</code>.</p>
</div>
<div class="paragraph">
<p>Whether you customize <code>DefaultOAuth2UserService</code> or provide your own implementation of <code>OAuth2UserService</code>, you&#8217;ll need to configure it as shown in the following example:</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
			    .userInfoEndpoint(userInfo -&gt; userInfo
			        .userService(this.oauth2UserService())
			        ...
			    )
			);
	}

	private OAuth2UserService&lt;OAuth2UserRequest, OAuth2User&gt; oauth2UserService() {
		...
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                userInfoEndpoint {
                    userService = oauth2UserService()
                    // ...
                }
            }
        }
    }

    private fun oauth2UserService(): OAuth2UserService&lt;OAuth2UserRequest, OAuth2User&gt; {
        // ...
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="oauth2login-advanced-oidc-user-service"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-oidc-user-service"></a>OpenID Connect 1.0 UserService</h4>
<div class="paragraph">
<p><code>OidcUserService</code> is an implementation of an <code>OAuth2UserService</code> that supports OpenID Connect 1.0 Provider&#8217;s.</p>
</div>
<div class="paragraph">
<p>The <code>OidcUserService</code> leverages the <code>DefaultOAuth2UserService</code> when requesting the user attributes at the UserInfo Endpoint.</p>
</div>
<div class="paragraph">
<p>If you need to customize the pre-processing of the UserInfo Request and/or the post-handling of the UserInfo Response, you will need to provide <code>OidcUserService.setOauth2UserService()</code> with a custom configured <code>DefaultOAuth2UserService</code>.</p>
</div>
<div class="paragraph">
<p>Whether you customize <code>OidcUserService</code> or provide your own implementation of <code>OAuth2UserService</code> for OpenID Connect 1.0 Provider&#8217;s, you&#8217;ll need to configure it as shown in the following example:</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.oauth2Login(oauth2 -&gt; oauth2
				.userInfoEndpoint(userInfo -&gt; userInfo
				    .oidcUserService(this.oidcUserService())
				    ...
			    )
			);
	}

	private OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; oidcUserService() {
		...
	}
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http {
            oauth2Login {
                userInfoEndpoint {
                    oidcUserService = oidcUserService()
                    // ...
                }
            }
        }
    }

    private fun oidcUserService(): OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; {
        // ...
    }
}</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-advanced-idtoken-verify"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-idtoken-verify"></a>ID Token Signature Verification</h3>
<div class="paragraph">
<p>OpenID Connect 1.0 Authentication introduces the <a href="https://openid.net/specs/openid-connect-core-1_0.html#IDToken">ID Token</a>, which is a security token that contains Claims about the Authentication of an End-User by an Authorization Server when used by a Client.</p>
</div>
<div class="paragraph">
<p>The ID Token is represented as a <a href="https://tools.ietf.org/html/rfc7519">JSON Web Token</a> (JWT) and MUST be signed using <a href="https://tools.ietf.org/html/rfc7515">JSON Web Signature</a> (JWS).</p>
</div>
<div class="paragraph">
<p>The <code>OidcIdTokenDecoderFactory</code> provides a <code>JwtDecoder</code> used for <code>OidcIdToken</code> signature verification. The default algorithm is <code>RS256</code> but may be different when assigned during client registration.
For these cases, a resolver may be configured to return the expected JWS algorithm assigned for a specific client.</p>
</div>
<div class="paragraph">
<p>The JWS algorithm resolver is a <code>Function</code> that accepts a <code>ClientRegistration</code> and returns the expected <code>JwsAlgorithm</code> for the client, eg. <code>SignatureAlgorithm.RS256</code> or <code>MacAlgorithm.HS256</code></p>
</div>
<div class="paragraph">
<p>The following code shows how to configure the <code>OidcIdTokenDecoderFactory</code> <code>@Bean</code> to default to <code>MacAlgorithm.HS256</code> for all <code>ClientRegistration</code>:</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Bean
public JwtDecoderFactory&lt;ClientRegistration&gt; idTokenDecoderFactory() {
	OidcIdTokenDecoderFactory idTokenDecoderFactory = new OidcIdTokenDecoderFactory();
	idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -&gt; MacAlgorithm.HS256);
	return idTokenDecoderFactory;
}</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@Bean
fun idTokenDecoderFactory(): JwtDecoderFactory&lt;ClientRegistration?&gt; {
    val idTokenDecoderFactory = OidcIdTokenDecoderFactory()
    idTokenDecoderFactory.setJwsAlgorithmResolver { MacAlgorithm.HS256 }
    return idTokenDecoderFactory
}</code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
For MAC based algorithms such as <code>HS256</code>, <code>HS384</code> or <code>HS512</code>, the <code>client-secret</code> corresponding to the <code>client-id</code> is used as the symmetric key for signature verification.
</td>
</tr>
</table>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<i class="fa icon-tip" title="Tip"></i>
</td>
<td class="content">
If more than one <code>ClientRegistration</code> is configured for OpenID Connect 1.0 Authentication, the JWS algorithm resolver may evaluate the provided <code>ClientRegistration</code> to determine which algorithm to return.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="oauth2login-advanced-oidc-logout"><a class="anchor" href="oauth2-login.html#oauth2login-advanced-oidc-logout"></a>OpenID Connect 1.0 Logout</h3>
<div class="paragraph">
<p>OpenID Connect Session Management 1.0 allows the ability to log out the End-User at the Provider using the Client.
One of the strategies available is <a href="https://openid.net/specs/openid-connect-session-1_0.html#RPLogout">RP-Initiated Logout</a>.</p>
</div>
<div class="paragraph">
<p>If the OpenID Provider supports both Session Management and <a href="https://openid.net/specs/openid-connect-discovery-1_0.html">Discovery</a>, the client may obtain the <code>end_session_endpoint</code> <code>URL</code> from the OpenID Provider&#8217;s <a href="https://openid.net/specs/openid-connect-session-1_0.html#OPMetadata">Discovery Metadata</a>.
This can be achieved by configuring the <code>ClientRegistration</code> with the <code>issuer-uri</code>, as in the following example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-yaml hljs" data-lang="yaml">spring:
  security:
    oauth2:
      client:
        registration:
          okta:
            client-id: okta-client-id
            client-secret: okta-client-secret
            ...
        provider:
          okta:
            issuer-uri: https://dev-1234.oktapreview.com</code></pre>
</div>
</div>
<div class="paragraph">
<p>&#8230;&#8203;and the <code>OidcClientInitiatedLogoutSuccessHandler</code>, which implements RP-Initiated Logout, may be configured as follows:</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock primary">
<div class="title">Java</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@EnableWebSecurity
public class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private ClientRegistrationRepository clientRegistrationRepository;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.authorizeRequests(authorize -&gt; authorize
				.anyRequest().authenticated()
			)
			.oauth2Login(withDefaults())
			.logout(logout -&gt; logout
				.logoutSuccessHandler(oidcLogoutSuccessHandler())
			);
	}

	private LogoutSuccessHandler oidcLogoutSuccessHandler() {
		OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
				new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);

		// Sets the location that the End-User's User Agent will be redirected to
		// after the logout has been performed at the Provider
		oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");

		return oidcLogoutSuccessHandler;
	}
}

NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
If used, the application's base URL, like `https://app.example.org`, will replace it at request time.</code></pre>
</div>
</div>
<div class="listingblock secondary">
<div class="title">Kotlin</div>
<div class="content">
<pre class="highlightjs highlight"><code class="language-kotlin hljs" data-lang="kotlin">@EnableWebSecurity
class OAuth2LoginSecurityConfig : WebSecurityConfigurerAdapter() {
    @Autowired
    private lateinit var clientRegistrationRepository: ClientRegistrationRepository

    override fun configure(http: HttpSecurity) {
        http {
            authorizeRequests {
                authorize(anyRequest, authenticated)
            }
            oauth2Login { }
            logout {
                logoutSuccessHandler = oidcLogoutSuccessHandler()
            }
        }
    }

    private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
        val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)

        // Sets the location that the End-User's User Agent will be redirected to
        // after the logout has been performed at the Provider
        oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
        return oidcLogoutSuccessHandler
    }
}

NOTE: `OidcClientInitiatedLogoutSuccessHandler` supports the `{baseUrl}` placeholder.
If used, the application's base URL, like `https://app.example.org`, will replace it at request time.</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<nav class="pagination">
<span class="prev"><a href="index.html">OAuth2</a></span>
<span class="next"><a href="oauth2-client.html">OAuth2 Client</a></span>
</nav>
</article>
</div>
</main>
</div>
<footer class="footer flex">
<div id="spring-links flex">
<img id="springlogo" src="../../../_/img/spring-logo.svg" alt="Spring">
<p class="smallest antialiased">© <script>var d = new Date();
        document.write(d.getFullYear());</script> <a href="https://www.vmware.com/">VMware</a>, Inc. or its affiliates. <a href="https://www.vmware.com/help/legal.html">Terms of Use</a> • <a href="https://www.vmware.com/help/privacy.html" rel="noopener noreferrer">Privacy</a> • <a href="https://spring.io/trademarks">Trademark Guidelines</a> <span id="thank-you-mobile">• <a href="https://spring.io/thank-you">Thank you</a></span> • <a href="https://www.vmware.com/help/privacy/california-privacy-rights.html">Your California Privacy Rights</a> • <a class="ot-sdk-show-settings">Cookie Settings</a> <span id="teconsent"></span></p>
<p class="smallest antialiased">Apache®, Apache Tomcat®, Apache Kafka®, Apache Cassandra&trade;, and Apache Geode&trade; are trademarks or registered trademarks of the Apache Software Foundation in the United States and/or other countries. Java&trade;, Java&trade; SE, Java&trade; EE, and OpenJDK&trade; are trademarks of Oracle and/or its affiliates. Kubernetes® is a registered trademark of the Linux Foundation in the United States and other countries. Linux® is the registered trademark of Linus Torvalds in the United States and other countries. Windows® and Microsoft® Azure are registered trademarks of Microsoft Corporation. “AWS” and “Amazon Web Services” are trademarks or registered trademarks of Amazon.com Inc. or its affiliates. All other trademarks and copyrights are property of their respective owners and are only mentioned for informative purposes. Other names may be trademarks of their respective owners.</p>
</div>
<div id="social-icons" class="flex jc-between">
<a href="https://www.youtube.com/user/SpringSourceDev" title="Youtube"><svg id="youtube-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><circle class="cls-1" cx="20" cy="20" r="20" /><path class="cls-2" d="M30.91,14.53a2.89,2.89,0,0,0-2-2C27.12,12,20,12,20,12s-7.12,0-8.9.47a2.9,2.9,0,0,0-2,2A30.56,30.56,0,0,0,8.63,20a30.44,30.44,0,0,0,.46,5.47,2.89,2.89,0,0,0,2,2C12.9,28,20,28,20,28s7.12,0,8.9-.47a2.87,2.87,0,0,0,2-2A30.56,30.56,0,0,0,31.37,20,28.88,28.88,0,0,0,30.91,14.53ZM17.73,23.41V16.59L23.65,20Z" /></svg></a>
<a href="https://github.com/spring-projects" title="Github"><svg id="github-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 75.93 75.93"><path class="cls-1" d="M38,0a38,38,0,1,0,38,38A38,38,0,0,0,38,0Z" /></g><path class="cls-2" d="M38,15.59A22.95,22.95,0,0,0,30.71,60.3c1.15.21,1.57-.5,1.57-1.11s0-2,0-3.9c-6.38,1.39-7.73-3.07-7.73-3.07A6.09,6.09,0,0,0,22,48.86c-2.09-1.42.15-1.39.15-1.39a4.81,4.81,0,0,1,3.52,2.36c2,3.5,5.37,2.49,6.67,1.91a4.87,4.87,0,0,1,1.46-3.07c-5.09-.58-10.45-2.55-10.45-11.34a8.84,8.84,0,0,1,2.36-6.15,8.29,8.29,0,0,1,.23-6.07s1.92-.62,6.3,2.35a21.82,21.82,0,0,1,11.49,0c4.38-3,6.3-2.35,6.3-2.35a8.29,8.29,0,0,1,.23,6.07,8.84,8.84,0,0,1,2.36,6.15c0,8.81-5.37,10.75-10.48,11.32a5.46,5.46,0,0,1,1.56,4.25c0,3.07,0,5.54,0,6.29s.42,1.33,1.58,1.1A22.94,22.94,0,0,0,38,15.59Z" /></svg></a>
<a href="https://twitter.com/springcentral" title="Twitter"><svg id="twitter-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 75.93 75.93"><circle class="cls-1" cx="37.97" cy="37.97" r="37.97" /><path id="Twitter-2" data-name="Twitter" class="cls-2" d="M55.2,22.73a15.43,15.43,0,0,1-4.88,1.91,7.56,7.56,0,0,0-5.61-2.49A7.78,7.78,0,0,0,37,30a7.56,7.56,0,0,0,.2,1.79,21.63,21.63,0,0,1-15.84-8.23,8,8,0,0,0,2.37,10.52,7.66,7.66,0,0,1-3.48-1v.09A7.84,7.84,0,0,0,26.45,41a7.54,7.54,0,0,1-2,.28A7.64,7.64,0,0,1,23,41.09a7.71,7.71,0,0,0,7.18,5.47,15.21,15.21,0,0,1-9.55,3.37,15.78,15.78,0,0,1-1.83-.11,21.41,21.41,0,0,0,11.78,3.54c14.13,0,21.86-12,21.86-22.42,0-.34,0-.68,0-1a15.67,15.67,0,0,0,3.83-4.08,14.9,14.9,0,0,1-4.41,1.24A7.8,7.8,0,0,0,55.2,22.73Z" /></svg></a>
</div>
</footer>
<script src="../../../_/js/site.js"></script>
<script async src="../../../_/js/vendor/highlight.js"></script>
<script async src="../../../_/js/vendor/tabs.js"></script>
<script src="../../../_/js/vendor/switchtheme.js"></script>
<script src="../../../_/js/vendor/docsearch.min.js"></script>

<script>
var search = docsearch({
  appId: '244V8V9FGG',
  apiKey: '82c7ead946afbac3cf98c32446154691',
  indexName: 'security-docs',
  inputSelector: '#search-input',
  autocompleteOptions: { hint: false, keyboardShortcuts: ['s'] },
  algoliaOptions: { hitsPerPage: 10 }
}).autocomplete
search.on('autocomplete:closed', function () { search.autocomplete.setVal() })
</script>
<script>if (window.parent == window) {(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-2728886-23', 'auto', {'siteSpeedSampleRate': 100});ga('send', 'pageview');}</script><script defer src="https://static.cloudflareinsights.com/beacon.min.js/v652eace1692a40cfa3763df669d7439c1639079717194" integrity="sha512-Gi7xpJR8tSkrpF7aordPZQlW2DLtzUlZcumS8dMQjwDHEnw9I7ZLyiOj/6tZStRBGtGgN6ceN6cMH8z7etPGlw==" data-cf-beacon='{"rayId":"702e4055eb149830","token":"bffcb8a918ae4755926f76178bfbd26b","version":"2021.12.0","si":100}' crossorigin="anonymous"></script>
</body>
</html>
